package com.yc.cinema.mapper;

import com.yc.cinema.entity.Hall;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CinemaMapper {

    // 如果查询到结果，返回一个对象，否则返回null
    Hall selectById(int id);

    // 实体对象的属性 => sql 参数
    @Insert("insert into hall values (default,#{name},#{is3d},#{size},#{seats})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int insertHall(Hall hall);

    // 实体对象的属性 => sql 参数
    @Update("update hall set name=#{name},is3d=#{is3d},size=#{size},seats=#{seats} where id=#{id}")
    void updateHall(Hall hall);

    @Delete("delete from hall where id=#{id}")
    void deleteHall(int id);

    // 实体对象的属性, 动态构建语句
    @Update("script" +
            "update hall\n" +
            "        <set>\n" +
            "        <if test=\"name != null\">\n" +
            "            name = #{name},\n" +
            "        </if>\n" +
            "        <if test=\"is3d != null\">\n" +
            "            is3d = #{is3d},\n" +
            "        </if>\n" +
            "        <if test=\"size != null\">\n" +
            "            size = #{size},\n" +
            "        </if>\n" +
            "        <if test=\"seats != null\">\n" +
            "            seats = #{seats},\n" +
            "        </if>\n" +
            "        </set>\n" +
            "        where id = #{id}" +
            "script")
    void updateHall1(Hall hall);

    //组合条件查询
    @Select("<script>" +
            " select * from hall\n" +
            "        <where>\n" +
            "            <if test=\"name != null\">\n" +
            "                and name like concat('%',#{name},'%')\n" +
            "            </if>\n" +
            "            <if test=\"is3d != null\">\n" +
            "                and is3d = #{is3d}\n" +
            "            </if>\n" +
            "            <if test=\"size != null\">\n" +
            "                and size = #{size}\n" +
            "            </if>\n" +
            "            <if test=\"seats != null\">\n" +
            "                and seats = #{seats}\n" +
            "            </if>\n" +
            "        </where>" +
            "<script>")
    List<Hall> selectByHall(Hall hall);


    List<Hall> selectByName(@Param("name") String name,@Param("state") int state);

    // id in (?,?,? ...)
    List<Hall> selectByIds(@Param("ids")Integer[] ids);

    List<Hall> selectOrderBy(@Param("size")String size,@Param("sort")String sort);

    List<Hall> selectBySql(@Param("sql") String sql);

}
